#include "CSetIntAttributeCommand.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"
#include "CSceneObject.h"
#include "IrrlichtDevice.h"
#include "IVideoDriver.h"
#include "Utils\Utils.h"


extern CGlobals* g;
extern CUserInterface* ui;
extern CLevelEditor* editor;

CSetIntAttributeCommand::CSetIntAttributeCommand (int id, stringc name, int newValue)
{
	m_id = id;
	m_name = name;
	m_oldValue = editor->getSceneObjectFromID(id)->m_UserData->getAttributeAsInt(name.c_str());
	m_newValue = newValue;
	setDescription(stringw(L"change ") + name.c_str());
}

void CSetIntAttributeCommand::redo ()
{
	CheckGridWayPointConnections(m_id, m_name, m_oldValue, m_newValue);

	editor->getSceneObjectFromID(m_id)->m_UserData->setAttribute(m_name.c_str(), m_newValue);
}

void CSetIntAttributeCommand::undo ()
{
	CheckGridWayPointConnections(m_id, m_name, m_newValue, m_oldValue);

	editor->getSceneObjectFromID(m_id)->m_UserData->setAttribute(m_name.c_str(), m_oldValue);	
}

void CSetIntAttributeCommand::CheckGridWayPointConnections(int id, stringc propName, int oldValue, int newValue)
{
	if (editor->getSceneObjectFromID(id)->m_UserData->getAttributeAsString("!GameType") == "Fiend" && propName == "$FiendTextureType")
	{
		array<stringw> pathArray = editor->getGameListArray("FiendTexturePath");			
		array<stringw> listArray = editor->getGameListArray("FiendTextures");
		
		if (pathArray.size() > 0)
		{
			stringw oldTexFile = pathArray[0] + listArray[oldValue];
			stringw newTexFile = pathArray[0] + listArray[newValue];

			ITexture* oldTex = g->device->getVideoDriver()->getTexture(oldTexFile.c_str());
			ITexture* newTex = g->device->getVideoDriver()->getTexture(newTexFile.c_str());

//cvp temp			ReplaceTexture(editor->getSceneObjectFromID(id)->m_SceneNode, oldTex, newTex);
		}
	}
	else if (editor->getSceneObjectFromID(id)->m_UserData->getAttributeAsString("!GameType") == "GridWayPoint")
	{
		if (propName == "^Left^GridWayPoint")
		{
			if (oldValue != -1)
			{
				editor->getSceneObjectFromID(oldValue)->m_UserData->setAttribute("^Right^GridWayPoint", -1);
			}

			CSceneObject* pNewObj = editor->getSceneObjectFromID(newValue);
			int newObjOldLink = pNewObj->m_UserData->getAttributeAsInt("^Right^GridWayPoint");

			if (newObjOldLink != -1)
			{
				editor->getSceneObjectFromID(newObjOldLink)->m_UserData->setAttribute("^Left^GridWayPoint", -1);
			}

			pNewObj->m_UserData->setAttribute("^Right^GridWayPoint", id);
		}

		if (propName == "^Right^GridWayPoint")
		{
			if (oldValue != -1)
			{
				editor->getSceneObjectFromID(oldValue)->m_UserData->setAttribute("^Left^GridWayPoint", -1);
			}

			CSceneObject* pNewObj = editor->getSceneObjectFromID(newValue);
			int newObjOldLink = pNewObj->m_UserData->getAttributeAsInt("^Left^GridWayPoint");

			if (newObjOldLink != -1)
			{
				editor->getSceneObjectFromID(newObjOldLink)->m_UserData->setAttribute("^Right^GridWayPoint", -1);
			}

			pNewObj->m_UserData->setAttribute("^Left^GridWayPoint", id);
		}

		if (propName == "^Up^GridWayPoint")
		{
			if (oldValue != -1)
			{
				editor->getSceneObjectFromID(oldValue)->m_UserData->setAttribute("^Down^GridWayPoint", -1);
			}

			CSceneObject* pNewObj = editor->getSceneObjectFromID(newValue);
			int newObjOldLink = pNewObj->m_UserData->getAttributeAsInt("^Down^GridWayPoint");

			if (newObjOldLink != -1)
			{
				editor->getSceneObjectFromID(newObjOldLink)->m_UserData->setAttribute("^Up^GridWayPoint", -1);
			}

			pNewObj->m_UserData->setAttribute("^Down^GridWayPoint", id);
		}

		if (propName == "^Down^GridWayPoint")
		{
			if (oldValue != -1)
			{
				editor->getSceneObjectFromID(oldValue)->m_UserData->setAttribute("^Up^GridWayPoint", -1);
			}

			CSceneObject* pNewObj = editor->getSceneObjectFromID(newValue);
			int newObjOldLink = pNewObj->m_UserData->getAttributeAsInt("^Up^GridWayPoint");

			if (newObjOldLink != -1)
			{
				editor->getSceneObjectFromID(newObjOldLink)->m_UserData->setAttribute("^Down^GridWayPoint", -1);
			}

			pNewObj->m_UserData->setAttribute("^Up^GridWayPoint", id);
		}
	}
}